package cn.com.dashihui.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Duang;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

import cn.com.dashihui.kit.DatetimeKit;
import cn.com.dashihui.kit.DirKit;
import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.kit.LuceneKit;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.Category;
import cn.com.dashihui.web.dao.GoodsSale;
import cn.com.dashihui.web.dao.GoodsSaleItem;
import cn.com.dashihui.web.dao.GoodsStore;
import cn.com.dashihui.web.service.GoodsStoreService;
import cn.com.dashihui.web.service.StoreService;

/**
 * 分公司商品管理<br/>
 * 管理员可对分公司商品进行管理，以及审核
 */
public class GoodsStoreController extends BaseController {
	private GoodsStoreService service = Duang.duang(GoodsStoreService.class);
	private StoreService storeService = Duang.duang(StoreService.class);

	/**
	 * 转到商品列表页面
	 */
	public void index() {
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		setAttr("RoleId", RoleId);
		if ( RoleId == 2) {
			String sellerId = null;
			if (StrKit.notNull(service.findByUserId(userid))) {
				sellerId = service.findByUserId(userid).get("sellerId");
			}
			Record record = service.countNumber(sellerId, RoleId);
			
			setAttr("tolnumber", record.get("tolnumber"));
			setAttr("sjshnumber", record.get("sjshnumber"));
			setAttr("sjnumber", record.get("sjnumber"));
			setAttr("xjnumber", record.get("xjnumber"));
			// List<Record> list=storeService.findAllSeller();
			// setAttr("list", list);
			List<Record> list = storeService.findAllStoreid(userid);
			setAttr("list", list);
			render("index.jsp");
		} else if(StrKit.notNull(storeService.findStoreState(userid))){
			String storeid = storeService.findStoreState(userid).get("storeid");
			Record record = service.countNumber(storeid, RoleId);
			
			setAttr("tolnumber", record.get("tolnumber"));
			setAttr("sjshnumber", record.get("sjshnumber"));
			setAttr("sjnumber", record.get("sjnumber"));
			setAttr("xjnumber", record.get("xjnumber"));
			List<Record> list = storeService.findAllSeller();
			setAttr("list", list);
			render("index.jsp");
		}

	}
	/**
	 * 列表页面异步请求数据
	 */
	public void page() {
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		int goodId = getParaToInt("goodId", 0);
		String sBeginDate = getPara("sBeginDate");
		String sEndDate = getPara("sEndDate");
		String xBeginDate = getPara("xBeginDate");
		String xEndDate = getPara("xEndDate");
		int state = getParaToInt("pstate", 0);
		String sGoodName = getPara("sGoodName");
		String sSellerId = null;
		String storeid = null;
		if (RoleId ==2&&StrKit.notNull(storeService.findByUserId(userid))) {
				sSellerId = storeService.findByUserId(userid).get("sellerId");
				storeid = getPara("storeid");
		}else if(StrKit.notNull(storeService.findStoreState(userid))){
			storeid = storeService.findStoreState(userid).get("storeid");
			sSellerId=storeService.findBysellerId(storeid).get("sellerId");
		}
		renderResult(0, service.findByPage(pageNum, pageSize,goodId, sGoodName, storeid, sBeginDate, sEndDate, xBeginDate,
				xEndDate, state, sSellerId));
	}
	/**
	 * 列表页面异步请求数据
	 */
	public void pageSort() {
		String index=getPara("index");
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		int goodId = getParaToInt("goodId", 0);
		String sBeginDate = getPara("sBeginDate");
		String sEndDate = getPara("sEndDate");
		String xBeginDate = getPara("xBeginDate");
		String xEndDate = getPara("xEndDate");
		int state = getParaToInt("pstate", 0);
		String sGoodName = getPara("sGoodName");
		String sSellerId = null;
		String storeid = null;
		if (RoleId ==2&&StrKit.notNull(storeService.findByUserId(userid))) {
				sSellerId = storeService.findByUserId(userid).get("sellerId");
				storeid = getPara("storeid");
		}else if(StrKit.notNull(storeService.findStoreState(userid))){
			storeid = storeService.findStoreState(userid).get("storeid");
			sSellerId=storeService.findBysellerId(storeid).get("sellerId");
		}
		renderResult(0, service.findByPageSort( index ,pageNum, pageSize, storeid, goodId, sGoodName, storeid, sBeginDate, sEndDate, xBeginDate,
				xEndDate, state, sSellerId));
	}
	/**
	 * 转到团购商品列表页面
	 */
	public void groupbuying() {
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		setAttr("RoleId", RoleId);
		if ( RoleId == 2) {
			String sellerId = null;
			if (StrKit.notNull(service.findByUserId(userid))) {
				sellerId = service.findByUserId(userid).get("sellerId");
			}
			Record record = service.countNumbergb(sellerId, RoleId);
			setAttr("gbnumber", record.get("gbnumber"));
			setAttr("tolnumber", record.get("tolnumber"));
			setAttr("sjshnumber", record.get("sjshnumber"));
			setAttr("sjnumber", record.get("sjnumber"));
			setAttr("xjnumber", record.get("xjnumber"));
			// List<Record> list=storeService.findAllSeller();
			// setAttr("list", list);
			List<Record> list = storeService.findAllStoreid(userid);
			setAttr("list", list);
			render("groupbuying.jsp");
		} else if(StrKit.notNull(storeService.findStoreState(userid))){
			String storeid = storeService.findStoreState(userid).get("storeid");
			Record record = service.countNumbergb(storeid, RoleId);
			setAttr("gbnumber", record.get("gbnumber"));
			setAttr("tolnumber", record.get("tolnumber"));
			setAttr("sjshnumber", record.get("sjshnumber"));
			setAttr("sjnumber", record.get("sjnumber"));
			setAttr("xjnumber", record.get("xjnumber"));
			List<Record> list = storeService.findAllSeller();
			setAttr("list", list);
			render("groupbuying.jsp");
		}
	}
	/*
	 * 列表页面异步请求团购数据数据
	 */
	public void pagegb() {
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		int goodId = getParaToInt("goodId", 0);
		String sBeginDate = getPara("sBeginDate");
		String sEndDate = getPara("sEndDate");
		String xBeginDate = getPara("xBeginDate");
		String xEndDate = getPara("xEndDate");
		int state = getParaToInt("pstate", 0);
		String sGoodName = getPara("sGoodName");
		String sSellerId = null;
		String storeid = null;
		if (RoleId ==2&&StrKit.notNull(storeService.findByUserId(userid))) {
				sSellerId = storeService.findByUserId(userid).get("sellerId");
				storeid = getPara("storeid");
		}else if(StrKit.notNull(storeService.findStoreState(userid))){
			storeid = storeService.findStoreState(userid).get("storeid");
			sSellerId=storeService.findBysellerId(storeid).get("sellerId");
		}
		renderResult(0, service.findByPagegb(pageNum, pageSize,goodId, sGoodName, storeid, sBeginDate, sEndDate, xBeginDate,
				xEndDate, state, sSellerId));
	}
	/**
	 * 列表页面异步请求团购数据
	 */
	public void pageSortGb() {
		String index=getPara("index");
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		int goodId = getParaToInt("goodId", 0);
		String sBeginDate = getPara("sBeginDate");
		String sEndDate = getPara("sEndDate");
		String xBeginDate = getPara("xBeginDate");
		String xEndDate = getPara("xEndDate");
		int state = getParaToInt("pstate", 0);
		String sGoodName = getPara("sGoodName");
		String sSellerId = null;
		String storeid = null;
		if (RoleId ==2&&StrKit.notNull(storeService.findByUserId(userid))) {
				sSellerId = storeService.findByUserId(userid).get("sellerId");
				storeid = getPara("storeid");
		}else if(StrKit.notNull(storeService.findStoreState(userid))){
			storeid = storeService.findStoreState(userid).get("storeid");
			sSellerId=storeService.findBysellerId(storeid).get("sellerId");
		}
		renderResult(0, service.findByPageSortGb( index ,pageNum, pageSize, storeid, goodId, sGoodName, storeid, sBeginDate, sEndDate, xBeginDate,
				xEndDate, state, sSellerId));
	}
	/**
	 * 普通商品 日、周、月销售统计
	 */
	public void xspage() {
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		int goodId = getParaToInt("goodId", 0);
		String sellerId = null;
		String storeid = null;
		if (RoleId == 1 || RoleId == 2) {
			sellerId = storeService.findByUserId(userid).get("sellerId");
			storeid = getPara("storeid");
		} else {
			storeid = storeService.findStoreState(userid).get("storeid");
		}
		String sBeginDate = getPara("sBeginDate");
		String sEndDate = getPara("sEndDate");
		String xBeginDate = getPara("xBeginDate");
		String xEndDate = getPara("xEndDate");
		int state = getParaToInt("pstate", 0);
		int date = getParaToInt("date", 0);
		renderResult(0, service.findXSByPage(pageNum, pageSize, goodId, sellerId, storeid, sBeginDate, sEndDate,
				xBeginDate, xEndDate, state, date));
	}
	/**
	 * 团购商品 日、周、月销售统计
	 */
	public void xspagegb(){
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		int goodId = getParaToInt("goodId",0);
		String sellerId = getPara("sellerId");
		String storeid = getPara("storeid");
		String sBeginDate = getPara("sBeginDate");
		String sEndDate = getPara("sEndDate");
		String xBeginDate = getPara("xBeginDate");
		String xEndDate = getPara("xEndDate");
		int state = getParaToInt("pstate",0);
		int date = getParaToInt("date",0);
		renderResult(0,service.findXSByPagegb(pageNum,pageSize,goodId,sellerId,storeid,sBeginDate,sEndDate,xBeginDate,xEndDate,state,date));
	}
	/**
	 * 交易产品详细内容页面
	 */
	public void toDetail() {
		int id = getParaToInt("id", 0);
		Record record=service.findGoodsById(id);
		setAttr("name", record.get("name"));
		String goodsName=storeService.goodsName(id);
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		setAttr("id", id);
		setAttr("RoleId", RoleId);
		setAttr("goodsName", goodsName);
		if (storeService.findByUserId(userid) != null) {
			if (RoleId == 2&&StrKit.notNull(service.findByUserId(userid))) {
				setAttr("sellerId", service.findByUserId(userid).get("sellerId"));
			}
		}
		render("JyDetail.jsp");
	}
	/**
	 * 商品订单
	 */
	public void goodsOrder(){
		int id = getParaToInt("id", 0);
		Record record=service.findGoodsById(id);
		setAttr("name", record.get("name"));
		String goodsName=storeService.goodsName(id);
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		setAttr("id", id);
		setAttr("RoleId", RoleId);
		setAttr("goodsName", goodsName);
		if (storeService.findByUserId(userid) != null) {
			if (RoleId == 2&&StrKit.notNull(service.findByUserId(userid))) {
				setAttr("sellerId", service.findByUserId(userid).get("sellerId"));
			}
		}
		render("goodsOrder.jsp");
	}
	/**
	 * 商品订单
	 */
	public void goodsOrdergb(){
		int id = getParaToInt("id", 0);
		Record record=service.findGoodsById(id);
		setAttr("name", record.get("name"));
		String goodsName=storeService.goodsName(id);
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		setAttr("id", id);
		setAttr("RoleId", RoleId);
		setAttr("goodsName", goodsName);
		if (storeService.findByUserId(userid) != null) {
			if (RoleId == 2&&StrKit.notNull(service.findByUserId(userid))) {
				setAttr("sellerId", service.findByUserId(userid).get("sellerId"));
			}
		}
		render("goodsOrdergb.jsp");
	}

	public void doDetail() {
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		// 商品id
		int id = getParaToInt("id", 0);
		// 订单状态
		int orderState = getParaToInt("orderState", 0);
		String sSellerId = null;
		String storeid = null;
		if (RoleId == 1 || RoleId == 2) {
			sSellerId = storeService.findByUserId(userid).get("sellerId");
			storeid = getPara("storeid");
		} else {
			storeid = storeService.findStoreState(userid).get("storeid");
			sSellerId=storeService.findBysellerId(storeid).get("sellerId");
		}
		renderResult(0, service.findOrderById(pageNum, pageSize, id, orderState, sSellerId, storeid));
	}
	public void doDetailgb() {
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		// 商品id
		int id = getParaToInt("id", 0);
		// 订单状态
		int orderState = getParaToInt("orderState", 0);
		String sSellerId = null;
		String storeid = null;
		if (RoleId == 1 || RoleId == 2) {
			sSellerId = storeService.findByUserId(userid).get("sellerId");
			storeid = getPara("storeid");
		} else {
			storeid = storeService.findStoreState(userid).get("storeid");
			sSellerId=storeService.findBysellerId(storeid).get("sellerId");
		}
		renderResult(0, service.findOrderByIdgb(pageNum, pageSize, id, orderState, sSellerId, storeid));
	}


	/**
	 * 商品降权
	 */
	public void tooDown() {
		String id = getPara("id");
		if (StrKit.isBlank(id)) {
			renderResult(1);
			return;
		}
		GoodsStore goods = service.findById(Integer.valueOf(id));
		goods.set("state", 5);
		goods.set("checktime", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));
		if (service.tooDown(goods)) {
			renderSuccess();
			return;
		}
	}

	/**
	 * 商品详情
	 */
//	public void goodDetail() {
//		int id = getParaToInt("id", 0);
//		if (id != 0) {
//			Record goods = service.findGoodDetailById(id);
//			String gooditem = goods.getStr("goodItem");
//			String good_store = goods.getStr("good_store");
//			if (!StrKit.isBlank(gooditem)) {
//				List<Record> goodItem = service.getGoodsItem(gooditem);
//				if (goodItem.size() > 0) {
//					setAttr("goodItem", goodItem);
//				}
//			}
//			if (!StrKit.isBlank(good_store)) {
//				List<Record> store = service.getStore(good_store);
//				if (store.size() > 0) {
//					setAttr("store", store);
//				}
//			}
//			setAttr("goods", goods);
//		}
//		render("detail.jsp");
//	}
	
	
	/**
	 * 商品信息页面
	 */
	public void goodDetail() {
		int id = getParaToInt("id", 0);
		int userid=getCurrentUser().getInt("id");
		if (id != 0) {
			Record goods = service.findGoodDetailById(id);
			String gooditem = goods.getStr("goodItem");
			String good_store = goods.getStr("good_store");
			// String LunBoNum=goods.getStr("LunBoNum");
			if (!StrKit.isBlank(gooditem)) {
				List<Record> goodItem = service.getGoodsItem(gooditem);
				if (goodItem.size() > 0) {
					setAttr("goodItem", goodItem);
				}
			}
			if (!StrKit.isBlank(good_store)) {
				List<Record> store = service.getStore(good_store);
				if (store.size() > 0) {
					setAttr("store", store);
				}
			}
			
				List<Record> goodSpecs = service.getGoodSpec(id);
				if (goodSpecs.size() > 0) {
					setAttr("goodSpec", goodSpecs);
				}
			
			setAttr("goods", goods);
		}
		List<GoodsSale> goodsSales = service.findAllSale(id);
		if (goodsSales.size() > 0) {
			setAttr("goodsSales", goodsSales);
		}
		setAttr("userid", userid);
		Record toker = service.findtoker(id);
		setAttr("toker", toker);
		List<Category> oneCategories = service.finOneCategory();
		setAttr("oneCategories", oneCategories);
		List<Category> twoCategories = service.findTwoCategory();
		setAttr("twoCategories", twoCategories);
		List<Category> threeCategories = service.findThreeCategory();
		setAttr("threeCategories", threeCategories);
		List<Category> fourCategories = service.findFourCategory();
		setAttr("fourCategories", fourCategories);
		List<GoodsSaleItem> saleItems = service.findSaleItem(id);
		setAttr("saleItems", saleItems);
		render("detail.jsp");
	}

	/**
	 * 分公司商品添加页面
	 */
	public void toAdd() {
		render("add.jsp");
	}

	/**
	 * 添加商品
	 */
	public void doAdd() {
		// 商品名称
		String name = getPara("name");
		// 所属类别：
		String categoryonid = getPara("categoryonid");
		String categorytwid = getPara("categorytwid");
		String categorythid = getPara("categorythid");
		String categoryfoid = getPara("categoryfoid");
		// 商品品牌
		String brandid = getPara("brandid", "0");
		// 商品规格说明
		String spec = getPara("spec");
		// 短简介
		String shortInfo = getPara("shortInfo");
		// 分公司价格
		String marketPrice = getPara("marketPrice");
		// 销售价格
		String sellPrice = getPara("sellPrice");
		// 状态
		int state = getParaToInt("state");
		// 优惠类型
		int type = getParaToInt("type");
		// 限购量
		String urv = getPara("urv");
		// 是否自营
		String isSelf = getPara("isSelf");
		if (StrKit.isBlank(name)) {
			renderResult(1);
			return;
		} else if (StrKit.isBlank(categoryonid) || StrKit.isBlank(categorytwid) || StrKit.isBlank(categorythid)
				|| StrKit.isBlank(categoryfoid)) {
			renderResult(2);
			return;
		} else if (StrKit.isBlank(spec)) {
			renderResult(3);
			return;
		} else if (StrKit.isBlank(marketPrice)) {
			renderResult(4);
			return;
		} else if (StrKit.isBlank(sellPrice)) {
			renderResult(5);
			return;
		} else if ((type == 3 || type == 4) && StrKit.isBlank(urv)) {
			renderResult(6);
			return;
		} else {
			GoodsStore goods = new GoodsStore().set("storeid", getCurrentUser().getInt("storeid")).set("name", name)
					.set("categoryonid", Integer.valueOf(categoryonid).intValue())
					.set("categorytwid", Integer.valueOf(categorytwid).intValue())
					.set("categorythid", Integer.valueOf(categorythid).intValue())
					.set("categoryfoid", Integer.valueOf(categoryfoid).intValue()).set("brandid", brandid)
					.set("spec", spec).set("shortInfo", shortInfo).set("marketPrice", marketPrice)
					.set("sellPrice", sellPrice).set("state", Integer.valueOf(state).intValue()).set("type", type)
					.set("isSelf", isSelf);
			if (type == 3 || type == 4) {
				goods.set("urv", Integer.valueOf(urv));
			}
			// 保存
			if (service.add(goods)) {
				// 生成索引
				LuceneKit.createIndex();
				renderSuccess();
				return;
			}
		}
		renderFailed();
	}

	/**
	 * 分公司产品修改商品信息页面
	 */
	public void toEdit() {
		int id = getParaToInt(0, 0);
		if (id != 0) {
			setAttr("object", service.findById(id));
		}
		render("edit.jsp");
	}

	public void edit() {
		int id = getParaToInt("id", 0);
		int pstate = getParaToInt("pstate", 0);
		if (id != 0 && pstate != 0) {
			GoodsStore goods = service.findById(id);
			goods.set("pstate", pstate);
			goods.set("checktime", DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));
			if (service.edit(goods)) {
				renderSuccess();
				return;
			} else {
				renderResult(1);
				return;
			}
		} else {
			renderResult(1);
			return;
		}
	}

	/**
	 * 分公司产品修改商品分销信息页面
	 */
	public void toEditRebate() {
		int id = getParaToInt(0, 0);
		if (id != 0) {
			setAttr("object", service.findById(id));
		}
		render("editorRebate.jsp");
	}

	/**
	 * 分公司产品修改商品分销信息并保存
	 */
	public void doEditRebate() {
		String id = getPara("goodsid");
		// 是否分销
		int isRebate = getParaToInt("isRebate", 0);
		// 推荐人返利比例
		double percent1 = Double.parseDouble(getPara("percent1"));
		// DSH返利比例
		double percent2 = Double.parseDouble(getPara("percent2"));
		// 社区负责人返利比例
		double percent3 = Double.parseDouble(getPara("percent3"));
		// 一级分销返利比例
		double percent4 = Double.parseDouble(getPara("percent4"));
		// 二级分销返利比例
		double percent5 = Double.parseDouble(getPara("percent5"));
		if (StrKit.isBlank(id)) {
			renderFailed();
			return;
		} else if (isRebate != 0 && isRebate != 1) {
			renderResult(1);// 参数isRebate不正确
			return;
		} else if (percent1 < 0 || percent1 > 99 || percent2 < 0 || percent2 > 99 || percent3 < 0 || percent3 > 99
				|| percent4 < 0 || percent4 > 99 || percent5 < 0 || percent5 > 99) {
			renderResult(2);// 参数percent1不正确
			return;
		} else {
			GoodsStore goods = new GoodsStore().set("id", id).set("isRebate", isRebate).set("percent1", percent1)
					.set("percent2", percent2).set("percent3", percent3).set("percent4", percent4)
					.set("percent5", percent5);
			// 保存
			if (service.update(goods)) {
				renderSuccess(service.findById(Integer.valueOf(id)));
				return;
			}
		}
		renderFailed();
	}

	/**
	 * 分公司产品修改商品信息并保存
	 */
	public void doEdit() {
		String id = getPara("goodsid");
		// 商品名称
		String name = getPara("name");
		// 所属类别：
		String categoryonid = getPara("categoryonid");
		String categorytwid = getPara("categorytwid");
		String categorythid = getPara("categorythid");
		String categoryfoid = getPara("categoryfoid");
		// 商品品牌
		String brandid = getPara("brandid", "0");
		// 商品规格说明
		String spec = getPara("spec");
		// 短简介
		String shortInfo = getPara("shortInfo");
		// 分公司价格
		String marketPrice = getPara("marketPrice");
		// 销售价格
		String sellPrice = getPara("sellPrice");
		// 状态
		int state = getParaToInt("state");
		// 优惠类型
		int type = getParaToInt("type");
		// 限购量
		String urv = getPara("urv");
		// 是否自营
		String isSelf = getPara("isSelf");
		if (StrKit.isBlank(id)) {
			renderFailed();
			return;
		} else if (StrKit.isBlank(name)) {
			renderResult(1);
			return;
		} else if (StrKit.isBlank(categoryonid) || StrKit.isBlank(categorytwid) || StrKit.isBlank(categorythid)
				|| StrKit.isBlank(categoryfoid)) {
			renderResult(2);
			return;
		} else if (StrKit.isBlank(spec)) {
			renderResult(3);
			return;
		} else if (StrKit.isBlank(marketPrice)) {
			renderResult(4);
			return;
		} else if (StrKit.isBlank(sellPrice)) {
			renderResult(5);
			return;
		} else if ((type == 3 || type == 4) && StrKit.isBlank(urv)) {
			renderResult(6);
			return;
		} else {
			GoodsStore goods = new GoodsStore().set("id", id).set("name", name)
					.set("categoryonid", Integer.valueOf(categoryonid).intValue())
					.set("categorytwid", Integer.valueOf(categorytwid).intValue())
					.set("categorythid", Integer.valueOf(categorythid).intValue())
					.set("categoryfoid", Integer.valueOf(categoryfoid).intValue()).set("brandid", brandid)
					.set("spec", spec).set("shortInfo", shortInfo).set("marketPrice", marketPrice)
					.set("sellPrice", sellPrice).set("state", Integer.valueOf(state).intValue()).set("type", type)
					.set("isSelf", isSelf);
			if (type == 3 || type == 4) {// 限量或者是一元购
				goods.set("urv", Integer.valueOf(urv));
			}
			// 保存
			if (service.update(goods)) {
				// 生成索引
				LuceneKit.createIndex();
				renderSuccess(service.findById(Integer.valueOf(id)));
				return;
			}
		}
		renderFailed();
	}

	public void doUpDownGoods() {
		String id = getPara("id");
		String flag = getPara("flag");
		if (StrKit.isBlank(id)) {
			renderResult(1);
			return;
		} else if (StrKit.isBlank(flag) || (!flag.equals("1") && !flag.equals("2"))) {
			renderResult(2);
			return;
		} else {
			// 更新
			GoodsStore goods = new GoodsStore();
			goods.set("id", Integer.valueOf(id).intValue());
			goods.set("state", flag);
			if (service.update(goods)) {
				// 生成索引
				LuceneKit.createIndex();
				renderSuccess();
				return;
			}
		}
	}

	/**
	 * 分公司产品删除指定id的商品记录<br/>
	 * 不删除图片
	 */
	public void doDelete() {
		int id = getParaToInt(0, 0);
		if (id != 0 && GoodsStore.me().deleteById(id)) {
			// 生成索引
			LuceneKit.createIndex();
			logger.info("删除商品【" + id + "】");
			renderSuccess();
			return;
		}
		renderFailed();

	}

	/**
	 * 分公司产品详情页面富文本编辑内容修改并保存数据
	 */
	public void doDetailEdit() {
		String id = getPara("id");
		String describe = getPara("describe");
		if (StrKit.isBlank(id)) {
			renderResult(1);
			return;
		}
		GoodsStore goods = new GoodsStore().set("id", Integer.valueOf(id).intValue()).set("describe", describe);
		if (service.update(goods)) {
			renderSuccess();
			return;
		}
		renderFailed();
	}

	/**
	 * 分公司产品文本编辑中图片上传
	 */
	public void uploadimg() {
		UploadFile thumb = getFile();
		Map<String, Object> map = new HashMap<String, Object>();
		if (thumb != null) {
			String thumbFileName;
			String dir = DirKit.getDir(DirKit.GOODS_DETAIL);
			try {
				thumbFileName = uploadToFtp(dir, thumb);
				map.put("error", 0);
				map.put("url", PropKit.get("constants.ftppath").concat(dir).concat(thumbFileName));
				renderJson(map);
				return;
			} catch (IOException e) {
				e.printStackTrace();
				logger.debug("上传图片失败");
				map.put("error", 1);
				map.put("message", "图片上传失败");
				renderJson(map);
			}
		} else {
			map.put("error", 1);
			map.put("message", "图片为空");
			renderJson(map);
		}
	}

	/**
	 * 图片列表
	 */
	public void imageInddex() {
		setAttr("goodsid", getParaToInt(0));
		render("imageList.jsp");
	}

	/**
	 * 图片排序
	 */
	public void doImageSort() {
		Map<String, String[]> params = getParaMap();
		if (params != null && params.size() != 0 && params.containsKey("sortKey")) {
			String sortKey = params.get("sortKey")[0];
			Map<String, String> sortMap = new HashMap<String, String>();
			for (String key : params.keySet()) {
				if (!key.equals("sortKey")) {
					String id = key.replace(sortKey, "");
					String no = params.get(key)[0];
					if (StrKit.isBlank(no) || no.length() > 3) {
						no = "0";
					}
					sortMap.put(id, no);
				}
			}
			/* service.sortImages(sortMap); */
			renderSuccess();
			return;
		}
		renderFailed();
	}

	/**
	 * 添加图片
	 */
	public void toImageAdd() {
		setAttr("goodsid", getPara(0));
		render("imageAdd.jsp");
	}

	/**
	 * 图片列表
	 */
	public void imageIndex() {
		setAttr("LunBoNum", getPara(0));
		render("imageList.jsp");
	}

	/**
	 * 图片分页
	 */
	public void imagePage() {
		renderSuccess(service.findAllImages(getPara(0)));
	}

	/**
	 * 添加详情图片列表
	 */
	public void imageDetailIndex() {
		setAttr("LunBoNum", getPara(0));
		render("detailImgList.jsp");
	}

	/**
	 * 详情图片分页
	 */
	public void imageDetailPage() {
		renderSuccess(service.findDetailImages(getPara(0)));
	}

	/**
	 * excel导出
	 */
	public void doExcel() {
		String operationMsg = "excel导出";
		logger.info(operationMsg);
		List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("id", "商品ID"));
		titles.add(new Pair("name", "商品名称"));
		titles.add(new Pair("title", "所属分公司"));
		titles.add(new Pair("countSell", "订单量"));
		/*titles.add(new Pair("stock", "库存数量"));
		titles.add(new Pair("paytype", "积分类型"));
		titles.add(new Pair("sellpoint", "所需积分"));
		titles.add(new Pair("sellPrice", "现金价格"));
		titles.add(new Pair("countSell", "订单量"));*/
		titles.add(new Pair("countamount", "销售额"));
		titles.add(new Pair("countCon", "收藏量"));
		titles.add(new Pair("countStore", "分公司数"));
		titles.add(new Pair("uptime", "上架时间"));
		titles.add(new Pair("downtime", "下架时间"));
		titles.add(new Pair("pstate", "当前状态"));
		titles.add(new Pair("tState", "拓客状态"));
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		int goodId = getParaToInt("goodId", 0);
		String sBeginDate = getPara("sBeginDate");
		String sEndDate = getPara("sEndDate");
		String xBeginDate = getPara("xBeginDate");
		String xEndDate = getPara("xEndDate");
		int state = getParaToInt("pstate", 0);
		int date = getParaToInt("date", 0);
		String sSellerId = null;
		String storeid = null;
		if (RoleId == 1 || RoleId ==2&& StrKit.notNull(storeService.findByUserId(userid))) {
			sSellerId = storeService.findByUserId(userid).get("sellerId");
			storeid = getPara("sStoreId");
		} else if(StrKit.notNull(storeService.findStoreState(userid))){
			storeid = storeService.findStoreState(userid).get("storeid");
			sSellerId=storeService.findBysellerId(storeid).get("sellerId");
		}

		// int state = getParaToInt("sState",0);
		List<Record> list = service.findExcelByPage(goodId, sSellerId, storeid, sBeginDate, sEndDate, xBeginDate,
				xEndDate, state, date);
		List<Record> listz = new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record = new Record();
			record.set("id", list.get(i).get("id"));
			record.set("name", list.get(i).get("name"));
			record.set("title", list.get(i).get("title"));
			record.set("countSell", list.get(i).get("countSell"));
			/*record.set("stock", list.get(i).get("stock"));
			if (list.get(i).get("paytype").equals(1)) {
				record.set("paytype", "货币");
			} else if (list.get(i).get("paytype").equals(2)) {
				record.set("paytype", "积分");
			} else if (list.get(i).get("paytype").equals(3)) {
				record.set("paytype", "抵扣券");
			} else if (list.get(i).get("paytype").equals(4)) {
				record.set("paytype", "惠券");
			}
			record.set("sellpoint", list.get(i).get("sellpoint"));
			record.set("sellPrice", list.get(i).get("sellPrice"));*/
			record.set("countSell", list.get(i).get("countSell"));
			record.set("countamount", list.get(i).get("countamount"));
			record.set("countCon", list.get(i).get("countCon"));
			record.set("countStore", list.get(i).get("countStore"));
			record.set("uptime", list.get(i).get("uptime"));
			record.set("downtime", list.get(i).get("downtime"));
			if (list.get(i).get("pstate").equals(1)) {
				if (list.get(i).get("state").equals(1)) {
					record.set("pstate", "上架审核中");
				} else if (list.get(i).get("state").equals(2)) {
					record.set("pstate", "上架");
				} else if (list.get(i).get("state").equals(3)) {
					record.set("pstate", "下架");
				}
			} else if (list.get(i).get("pstate").equals(2)) {
				record.set("pstate", "售罄");
			} else if (list.get(i).get("pstate").equals(3)) {
				record.set("pstate", "降权");
			}
			if (list.get(i).get("tState")!=null&&list.get(i).get("tState").equals(1)) {
				record.set("tState", "打开");
			}else {
				record.set("tState", "关闭");
			}
			 
			listz.add(record);
		}
		// 导出
		service.export(getResponse(), getRequest(), titles, listz);
		logger.info("成功");
		renderNull();
	}

	/**
	 * excel导出
	 */
	public void doExceljy() {
		String operationMsg = "excel导出";
		logger.info(operationMsg);
		List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("orderNum", "订单号"));
		titles.add(new Pair("name", "买家姓名"));
		titles.add(new Pair("tel", "联系方式"));
		titles.add(new Pair("countnum", "兑换数量"));
		titles.add(new Pair("startDate", "下单时间"));
		titles.add(new Pair("expireDate", "兑换有效期"));
		titles.add(new Pair("dhstore", "兑换分公司"));
		titles.add(new Pair("deliverDate", "兑换时间"));
		titles.add(new Pair("payState", "状态"));
		int userid = getCurrentUser().getInt("id");
		int RoleId = storeService.findByRoleId(userid);
		// 商品id
		int id = getParaToInt("id", 0);
		// 订单状态
		int orderState = getParaToInt("orderState", 0);
		String sSellerId = null;
		String storeid = null;
		if (RoleId == 1 || RoleId == 2&&StrKit.notNull(storeService.findByUserId(userid))) {
			sSellerId = storeService.findByUserId(userid).get("sellerId");
			storeid = getPara("sStoreId");
		} else if(StrKit.notNull(storeService.findStoreState(userid))){
			storeid = storeService.findStoreState(userid).get("storeid");
			sSellerId=storeService.findBysellerId(storeid).get("sellerId");
		}
		List<Record> list = storeService.jyfindExcelByPage(id, orderState, sSellerId, storeid);
		List<Record> listz = new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record = new Record();
			record.set("orderNum", list.get(i).get("orderNum"));
			record.set("name", list.get(i).get("name"));
			record.set("tel", list.get(i).get("tel"));
			/// record.set("xdstore", list.get(i).get("xdstore"));
			// record.set("goodsid", list.get(i).get("goodsid"));
			// record.set("goodsName", list.get(i).get("goodsName"));
			record.set("countnum", list.get(i).get("countnum"));
			record.set("startDate", list.get(i).get("startDate"));
			/*
			 * if(list.get(i).get("pointType").equals(1)){
			 * record.set("pointType", "积分"); }else
			 * if(list.get(i).get("pointType").equals(2)){
			 * record.set("pointType", "抵扣券"); }else
			 * if(list.get(i).get("pointType").equals(3)){
			 * record.set("pointType", "惠券"); }
			 */
			// record.set("payPoint", list.get(i).get("payPoint"));
			// record.set("amount", list.get(i).get("amount"));
			record.set("expireDate", list.get(i).get("expireDate"));
			record.set("dhstore", list.get(i).get("dhstore"));
			record.set("deliverDate", list.get(i).get("deliverDate"));
			if (list.get(i).get("payState").equals(1)) {
				record.set("payState", "未支付");
			}
			if (list.get(i).get("payState").equals(2)) {
				if (list.get(i).get("orderState").equals(1)) {
					record.set("payState", "未兑换");
				} else if (list.get(i).get("orderState").equals(2)) {
					record.set("payState", "已付款");
				} else if (list.get(i).get("orderState").equals(3)) {
					record.set("payState", "已过期");
				} else if (list.get(i).get("orderState").equals(4)) {
					record.set("payState", "已删除");
				}
			}
			listz.add(record);
		}
		// 导出
		storeService.jyexport(getResponse(), getRequest(), titles, listz);
		logger.info("成功");
		renderNull();
	}

	/**
	 * 添加图片
	 * 
	 * @return -1：异常，0：成功，1：图片为空，2：图片上传失败
	 */
	/*
	 * public void doImageAdd(){ UploadFile thumb = getFile("thumb"); int
	 * goodsid = getParaToInt("goodsid"); //保存 //如果上传了图片，则上传至FTP，并记录图片文件名
	 * if(thumb!=null){ String thumbFileName; String dir =
	 * DirKit.getDir(DirKit.GOODS_IMAGES); try { thumbFileName =
	 * uploadToFtp(dir,thumb); } catch (IOException e) { e.printStackTrace();
	 * renderResult(2); return; } StoreImages image = new
	 * StoreImages().set("goodsid", goodsid).set("thumb",
	 * dir.concat(thumbFileName)); if(service.addImage(image)){
	 * renderSuccess(image); return; } }else{ renderResult(1); return; } }
	 */
	/**
	 * 跳转收藏此商品的用户信息页面
	 */
	public void countCon(){
		int goodsId=getParaToInt("goodsId", 0);		
		setAttr("goodsId",goodsId);
		setAttr("goods",service.findGoodsById(goodsId));
		render("collect.jsp");
	}
	/**
	 * 收藏此商品的用户信息列表
	 */
	public void doCountCon(){
		int goodsId=getParaToInt("goodsId", 0);	
		int pageNum = getParaToInt(0, 1);
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		renderResult(0,service.findGoodsByCountCon(pageNum,pageSize,goodsId));	
	}
	/**
	 * excel导出 收藏此商品的用户信息列表
	 */
	public void doCollectExcel() {
		String operationMsg = "excel导出";
		logger.info(operationMsg);
		List<Pair> titles = new ArrayList<Pair>();
		titles.add(new Pair("id", "商品ID"));
		titles.add(new Pair("name", "会员名称"));
		titles.add(new Pair("phone", "会员手机号"));
		titles.add(new Pair("store", "所属分公司"));
		titles.add(new Pair("toBossOneId", "办卡销售"));
		titles.add(new Pair("createDate", "办卡日期"));
		titles.add(new Pair("state", "会与状态"));
		titles.add(new Pair("csptnum", "消费次数"));
		titles.add(new Pair("amount", "消费总金额"));
		titles.add(new Pair("sppoint", "积分"));
		titles.add(new Pair("jkpoint", "抵扣券"));
		titles.add(new Pair("jypoint", "惠券"));
		titles.add(new Pair("excnum", "兑换次数"));
		
		int goodsId = getParaToInt("goodsId",0);
		List<Record> list = service.findGoodsByCountConExcel(goodsId);
		List<Record> listz = new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record = new Record();
			record.set("id", list.get(i).get("id"));
			record.set("name", list.get(i).get("name"));
			record.set("phone", list.get(i).get("phone"));
			record.set("store", list.get(i).get("store"));
			
			record.set("toBossOneId", list.get(i).get("toBossOneId"));
			record.set("createDate", list.get(i).get("createDate"));
			record.set("state", list.get(i).get("state"));
			record.set("csptnum", list.get(i).get("csptnum"));
			record.set("amount", list.get(i).get("amount"));
			record.set("sppoint", list.get(i).get("sppoint"));
			record.set("jkpoint", list.get(i).get("jkpoint"));
			record.set("jypoint", list.get(i).get("jypoint"));			
			record.set("excnum", list.get(i).get("excnum"));
			listz.add(record);
		}
		// 导出
		service.export(getResponse(), getRequest(), titles, listz);
		logger.info("成功");
		renderNull();
	}
}
